package com.ruoyi.project.dict.shuowenmapper.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.shuowenmapper.domain.ShuowenMapper;
import com.ruoyi.project.dict.shuowenmapper.service.IShuowenMapperService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 难检匹配Controller
 *
 * @author shixiangwen
 * @date 2022-06-02
 */
@Controller
@RequestMapping("/dict/shuowenmapper")
public class ShuowenMapperController extends BaseController {
    private String prefix = "dict/shuowenmapper";

    @Autowired
    private IShuowenMapperService shuowenMapperService;

    @Autowired
    @Qualifier("ssdbTemplate")
    RedisTemplate<String, String> redisTemplate;

    @RequiresPermissions("dict:shuowenmapper:view")
    @GetMapping()
    public String shuowenmapper() {
        return prefix + "/shuowenmapper";
    }

    /**
     * 查询难检匹配列表
     */
    @RequiresPermissions("dict:shuowenmapper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShuowenMapper shuowenMapper) {
        startPage();
        List<ShuowenMapper> list = shuowenMapperService.selectShuowenMapperList(shuowenMapper);
        return getDataTable(list);
    }

    /**
     * 导出难检匹配列表
     */
    @RequiresPermissions("dict:shuowenmapper:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ShuowenMapper shuowenMapper) {
        List<ShuowenMapper> list = shuowenMapperService.selectShuowenMapperList(shuowenMapper);
        ExcelUtil<ShuowenMapper> util = new ExcelUtil<ShuowenMapper>(ShuowenMapper.class);
        return util.exportExcel(list, "shuowenmapper");
    }

    /**
     * 新增难检匹配
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存难检匹配
     */
    @RequiresPermissions("dict:shuowenmapper:add")
    @Log(title = "难检匹配", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShuowenMapper shuowenMapper) {
        return toAjax(shuowenMapperService.insertShuowenMapper(shuowenMapper));
    }

    /**
     * 修改难检匹配
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ShuowenMapper shuowenMapper = shuowenMapperService.selectShuowenMapperById(id);
        mmap.put("shuowenMapper", shuowenMapper);
        return prefix + "/edit";
    }

    /**
     * 修改保存难检匹配
     */
    @RequiresPermissions("dict:shuowenmapper:edit")
    @Log(title = "难检匹配", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ShuowenMapper shuowenMapper) {
        return toAjax(shuowenMapperService.updateShuowenMapper(shuowenMapper));
    }

    @GetMapping("/compare/{id}")
    public String compare(@PathVariable("id") Long id, ModelMap mmap) {
        ShuowenMapper shuowenMapper = shuowenMapperService.selectShuowenMapperById(id);

        String syjz = (String) redisTemplate.opsForHash().get("syjz", shuowenMapper.getZi());
        mmap.put("syjz", syjz);
        mmap.put("img", "shuowen/n" + shuowenMapper.getZiOutId() + ".jpg");
        return prefix + "/compare";
    }

    /**
     * 删除难检匹配
     */
    @RequiresPermissions("dict:shuowenmapper:remove")
    @Log(title = "难检匹配", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(shuowenMapperService.deleteShuowenMapperByIds(ids));
    }

    @RequiresPermissions("dict:shuowenmapper:edit")
    @Log(title = "数据打包", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(Long id, Integer status) {
        return toAjax(shuowenMapperService.updateStatus(id, status));
    }
}
